package com.renwei.yunlong.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.renwei.yunlong.R;
import com.renwei.yunlong.fragment.ContactsFragment;
import com.renwei.yunlong.fragment.MeFragment;
import com.renwei.yunlong.fragment.NewsFragment;
import com.renwei.yunlong.fragment.WorkOrderFragment;
import com.renwei.yunlong.widget.BadgeRadioButton;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private BadgeRadioButton mWorkBtn,mNewsBtn,mCommBtn,mMeBtn;

    private FrameLayout frameLayout;

    private WorkOrderFragment workFragment;
    private NewsFragment newsFragment;
    private ContactsFragment contactsFragment;
    private MeFragment meFragment;
    private List<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);
        
        initView();
    }

    private void initView() {
        mWorkBtn = (BadgeRadioButton) findViewById(R.id.rb_work);
        mNewsBtn = (BadgeRadioButton) findViewById(R.id.rb_news);
        mCommBtn = (BadgeRadioButton) findViewById(R.id.rb_comm);
        mMeBtn = (BadgeRadioButton) findViewById(R.id.rb_me);

        frameLayout = (FrameLayout) findViewById(R.id.frame_layout);

        //创建Fragment对象及集合
        workFragment = new WorkOrderFragment();
        newsFragment = new NewsFragment();
        contactsFragment = new ContactsFragment();
        meFragment = new MeFragment();

        //将Fragment对象添加到list中
        list = new ArrayList<>();
        list.add(workFragment);
        list.add(newsFragment);
        list.add(contactsFragment);
        list.add(meFragment);

        //初始时向容器中添加第一个Fragment对象
        addFragment(workFragment);

        mWorkBtn.setOnClickListener(this);
        mNewsBtn.setOnClickListener(this);
        mCommBtn.setOnClickListener(this);
        mMeBtn.setOnClickListener(this);

    }

    //向Activity中添加Fragment的方法
    public void addFragment(Fragment fragment) {
        //获得Fragment管理器
        FragmentManager fragmentManager = getSupportFragmentManager();
        //使用管理器开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //使用事务替换Fragment容器中Fragment对象
        fragmentTransaction.replace(R.id.frame_layout,fragment);
        //提交事务，否则事务不生效
        fragmentTransaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_work:
                addFragment(workFragment);
                break;
            case R.id.rb_news:

                addFragment(newsFragment);
                break;
            case R.id.rb_comm:

                addFragment(contactsFragment);
                break;
            case R.id.rb_me:

                addFragment(meFragment);
                break;
            default:
                break;
        }
    }
}
